var isRefreshOddsTable = true;
var strAgent  = navigator.userAgent.toLowerCase();
var isNN = (strAgent.indexOf("netscape")!=-1);
var isOpera = (strAgent.indexOf("opera")!=-1);
var isIE = (strAgent.indexOf("msie") != -1);

function autoTab(eCtrl, len, e){
	var keyCode;
	var filter;
	var strWGold = document.ShowOddsForm["WGold"].value;
	if(!strWGold.match(/^([0-9])+$/))strWGold == "";

	if(isNN){
		keyCode = e.which;
		filter = [0,8,9];
	}else{
		keyCode = e.keyCode;
		filter = [0,8,9,16,17,18,37,38,39,40,46];
	}

	if(eCtrl.value.length >= len && !containsElement(filter,keyCode)){
		eCtrl.value = eCtrl.value.slice(0, len);
		eCtrl.form[(getIndex(eCtrl)+1) % eCtrl.form.length].focus();

		if(strWGold != "" && eCtrl.form[(getIndex(eCtrl)+1) % eCtrl.form.length].name.indexOf("Stake") >= 0){
			if(eCtrl.form[(getIndex(eCtrl)) % eCtrl.form.length].value != ""){
				eCtrl.form[(getIndex(eCtrl)+1) % eCtrl.form.length].value = strWGold;
				eCtrl.form[(getIndex(eCtrl)+2) % eCtrl.form.length].focus();
			}
		}
	}

	function containsElement(arr, ele){
		var found = false, index = 0;
		while(!found && index < arr.length)
			if(arr[index] == ele)
				found = true;
			else
				index++;

		return found;
	}

	function getIndex(eCtrl){
		var index = -1, i = 0, found = false;
		while (i < eCtrl.form.length && index == -1)
			if (eCtrl.form[i] == eCtrl) index = i;
			else i++;

		return index;
	}
	return true;
}

function GetWagerTable(){
	if(isGameStart == "N")return false;
	if(isRefreshOddsTable == false)return ClearTextField();

	var objWTable = document.getElementById("wager_table"), i = 0, j = 0, intNum = 0, strNum = "", strDisabled = "", StopClass = "";
	var ArrStake = new Array();

	with(objWTable)	{
		for(intNum = 0; intNum <= 119; intNum++)	{
			if(null != document.ShowOddsForm["Odds"+intNum.toString()]){
				ArrStake[intNum] = document.ShowOddsForm["Odds"+intNum.toString()].value;
			}else{
				ArrStake[intNum] = "";
			}

		}
		while(rows.length > 1)	deleteRow(rows.length - 1);
			
		for(i = 0; i <= 9; i++)	{
			newTR = insertRow(rows.length);

			var intCellNo = 0;
			for(j = 0; j <= 5; j++)	{
				intNum = j *10 + i;
				
				newTR.className = "mcen";
				with(newTR){
					newTD = insertCell(intCellNo);
					newTD.id = "NumberValue"+intNum.toString();
					newTD.className = "mcen_th";
					newTD.innerHTML = "<input name=\"ChoiceNumber["+intNum.toString()+"]\" value=\"\" class=\"zamoney\" type=\"text\" size=\"1\" style=\"background-color:  #CCFFFF;line-height: 12px; border: 1px solid #33CCFF\" maxlength=\"4\" onkeyup=\"return autoTab(this, 4, event);\">";
					intCellNo++;

					newTD = insertCell(intCellNo);
					newTD.id = "StakeValue"+intNum.toString();
					newTD.className = "mbline";
					newTD.innerHTML = "<input name=\"ChoiceStake["+intNum.toString()+"]\" value=\"\" class=\"zamoney\" type=\"text\" size=\"1\" style=\"line-height: 12px\" onkeyup=\"return autoTab(this, 4, event);\">";
					intCellNo++;
				} //with(newTR)
			}
		}
	} //with(objWTable)
	isRefreshOddsTable = false;
}

function CheckNumKey(){
	return false;
}

function InitAction(){
	GetWagerTable();
}

function ChoiceAllNumber(isCheck){
	with(document.ShowOddsForm){
		for(var i = 0; i < ChoiceNo.length; i++){
			ChoiceNo[i].checked = isCheck;
		}
	}
}

function ClearTextField(){
	var i = 0, j = 0;
	for(i = 0; i <= 9; i++)	{
		for(j = 0; j <= 5; j++)	{
			intNum = j *10 + i;
			document.ShowOddsForm["ChoiceNumber["+intNum.toString()+"]"].value = '';
			document.ShowOddsForm["ChoiceStake["+intNum.toString()+"]"].value = '';
		}
	}
	document.HideOddsForm["ChoiceNumber"].value = '';
	return true;
}

var arrCNo = new Array(10);
/*function CombinationNum(){
	ClearTextField();

	var intCType = 0;
	var arrCType = new Array('ooxx', 'oxox', 'oxxo', 'xoox', 'xoxo', 'xxoo', 'ooox', 'ooxo', 'oxoo', 'xooo', 'oooo');
	var strCType = '';
	for(var i = 0; i < document.ShowOddsForm.ChoiceType.length; i++){
		if(document.ShowOddsForm.ChoiceType[i].checked == true){
			intCType = document.ShowOddsForm.ChoiceType[i].value;
			strCType = arrCType[i];
		}
	}

	var k = 0; //組數
	for(var i = 0; i < 10; i++){
		if(eval("document.ShowOddsForm.ChoiceNo["+i+"].checked") == true){
			arrCNo[k] = i;
			k++;
		}
	}

	var strAllNum = '';
	var strNum = '';
	if(intCType > 20){	//四定位
		if(k < 4)return false;

		for(var i1 = 0; i1 < k; i1++){
			for(var i2 = 0; i2 < k; i2++){
				for(var i3 = 0; i3 < k; i3++){
					for(var i4 = 0; i4 < k; i4++){
						strAllNum += arrCNo[i1].toString() + arrCNo[i2].toString() + arrCNo[i3].toString() + arrCNo[i4].toString() + ',';
					}
				}
			}
		}

	}else if(intCType > 10){	//三定位
		if(k < 3)return false;

		for(var i1 = 0; i1 < k; i1++){
			for(var i2 = 0; i2 < k; i2++){
				for(var i3 = 0; i3 < k; i3++){
					strNum = strCType;
					strNum = strNum.replace("o", arrCNo[i1]);
					strNum = strNum.replace("o", arrCNo[i2]);
					strNum = strNum.replace("o", arrCNo[i3]);
					strAllNum += strNum + ',';
				}
			}
		}

	}else{	//二定位
		if(k < 2)return false;
		for(var i1 = 0; i1 < k; i1++){	
			for(var i2 = 0; i2 < k; i2++){
				strNum = strCType;
				strNum = strNum.replace("o", arrCNo[i1]);
				strNum = strNum.replace("o", arrCNo[i2]);
				strAllNum += strNum + ',';
			}
		}
	}

	if(strAllNum.length > 0){strAllNum = strAllNum.substring(0, strAllNum.length - 1);}
	var arrAllNum = new Array();
	arrAllNum = strAllNum.split(",");
	for (var intNum = 0; intNum < arrAllNum.length; intNum++) {
		if(intNum > 59)break;
		document.ShowOddsForm["ChoiceNumber["+intNum.toString()+"]"].value = arrAllNum[intNum];
		document.ShowOddsForm["ChoiceStake["+intNum.toString()+"]"].value = document.ShowOddsForm.WGold.value;
	}
}*/

function CombinationNum(){
	var strWNum = document.ShowOddsForm["WNum"].value;
	if(!strWNum.match(/^([0-9]){4}$/))return false;

	ClearTextField();
	var strAllNum = '';
	var strNum = '';
	for(var i = 0; i < strWNum.length; i++) {
		for(var ii = 0; ii < strWNum.length; ii++) {
			if(i == ii)continue;

			for(var iii = 0; iii < strWNum.length; iii++) {
				if(i == iii || ii == iii)continue;

				for(var iiii = 0; iiii < strWNum.length; iiii++) {
					if(i == iiii || ii == iiii || iii == iiii)continue;

					strNum = strWNum.charAt(i) + strWNum.charAt(ii) + strWNum.charAt(iii) + strWNum.charAt(iiii);
					if(strAllNum.indexOf(strNum) >= 0)continue;
					strAllNum += strNum + ',';
				}
			}
		}
	}

	if(strAllNum.length > 0){strAllNum = strAllNum.substring(0, strAllNum.length - 1);}
	var arrAllNum = new Array();
	arrAllNum = strAllNum.split(",");
	for (var intNum = 0; intNum < arrAllNum.length; intNum++) {
		if(intNum > 59)break;
		document.ShowOddsForm["ChoiceNumber["+intNum.toString()+"]"].value = arrAllNum[intNum];
		document.ShowOddsForm["ChoiceStake["+intNum.toString()+"]"].value = document.ShowOddsForm.WGold.value;
	}
}

function InputGoldToNum(){
	if(document.ShowOddsForm["WGold"].value == "")return false;

	for (var intNum = 0; intNum < 60; intNum++) {
		if(document.ShowOddsForm["ChoiceNumber["+intNum.toString()+"]"].value != "")document.ShowOddsForm["ChoiceStake["+intNum.toString()+"]"].value = document.ShowOddsForm["WGold"].value;
	}
}

function FinalCheck(){
	var objNum = "", objWGold = "", strAllNum = '';
	var patternNum = /^([x0-9])+/;

	for (var intNum = 0; intNum < 60; intNum++) {
		objNum = document.ShowOddsForm["ChoiceNumber["+intNum.toString()+"]"];
		objWGold = document.ShowOddsForm["ChoiceStake["+intNum.toString()+"]"];

		if(objNum.value == "" && objWGold.value == "")continue;
		if(objNum.value == "" && objWGold.value != ""){
			alert(strInputNumber);
			strInputNumber

			objNum.focus();
			return false;

		}else if(objNum.value != "" && objWGold.value == ""){
			alert(strInputGold);
			objWGold.focus();
			return false;
		}

		if(!objNum.value.match(/^([0-9\*x]){4}$/)){
			if(!objNum.value.match(/^([0-9]){2,3}$/)){
				alert(strNumberErr);
				objNum.focus();
				return false;
			}
		}

		if(objNum.value.match(/([\*x]{3,4})/)){
			alert(strNumberErr);
			objNum.focus();
			return false;
		}

		if(isNaN(objWGold.value)){
			alert(strGoldErr);
			objWGold.focus();
			return false;
		}

		strAllNum += objNum.value + "@" + objWGold.value + ",";
	}
	if(strAllNum == "")return false;
	strAllNum = strAllNum.substring(0, strAllNum.length - 1);
	document.HideOddsForm["ChoiceNumber"].value = strAllNum;
	document.HideOddsForm.submit();
}